package com.crm.service.permission;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.permission.CrmDeptDAO;
import com.crm.dao.permission.UserInfoDAO;
import com.crm.exception.ServiceException;
import com.crm.pojo.CrmDept;
import com.crm.pojo.Userinfo;

@Service
@Transactional(rollbackFor = Exception.class)
public class PowerService {

	@Autowired
	private CrmDeptDAO crmDeptDAO;
	
	@Autowired
	private UserInfoDAO userInfoDAO;
	
	public List<Userinfo> findDeptUsers(Long crmDeptno){
		return userInfoDAO.queryDeptUser(crmDeptno);
	}
	

	/**
	 * 查询所有部门
	 * 
	 * @return
	 */
	public List<CrmDept> findAllCrmDept() {
		return (List<CrmDept>) crmDeptDAO.queryAllDept();
	}

	
	
	public CrmDept findOneCrmDept(String crmDname) {
		return crmDeptDAO.queryOneCrmDept(crmDname);
	}

	/**
	 * 分页查询所有部门
	 * 
	 * @param curpage
	 * @param pagesize
	 * @return
	 */
	public List<CrmDept> queryPage(int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return crmDeptDAO.queryPage(page);
	}

	/**
	 * 查询所有记录数
	 * 
	 * @return
	 * @throws ServiceException
	 */
	public Long queryPageCount() throws ServiceException {
		return crmDeptDAO.queryShiCount();
	}

	public Long queryPageNmaeCount(String crmDname) throws ServiceException {
		return crmDeptDAO.queryShiNmeCount("%" + crmDname + "%");
	}
	/**
	 * 添加部门
	 *
	 * @param crmDeptno
	 * @param crmDname
	 * @return
	 */
	public void AddCrmDept(String crmDname) throws ServiceException {
		try {
			CrmDept cd = new CrmDept();
			cd.setCrmDname(crmDname);
			cd.setIsDel("是");
			crmDeptDAO.save(cd);

		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 根据部门名称模糊查询
	 * 
	 * @param crmDname
	 * @return
	 */
	public List<CrmDept> querySomeCrmDept(String crmDname,int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return crmDeptDAO.queryCrmDept("%" + crmDname + "%",page);
	}

	/**
	 * 逻辑删除一个部门
	 * 
	 * @param crmDeptno
	 */
	public void deleteOneCrmDept(Long crmDeptno) throws ServiceException {
		try {
			CrmDept cd = crmDeptDAO.findOne(crmDeptno);
			cd.setIsDel("否");
			crmDeptDAO.save(cd);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException();
		}
	}

	/**
	 * 根据部门ID查找部门
	 * 
	 * @param crmDeptno
	 * @return
	 */
	public CrmDept queryOne(Long crmDeptno) {
		CrmDept cd = crmDeptDAO.findOne(crmDeptno);
		return cd;
	}

	/**
	 * 修改部门信息
	 * 
	 * @param crmDeptno
	 * @param crmDname
	 * @param isDel
	 */
	public void updateOneCrmDept(Long crmDeptno, String crmDname) {
		CrmDept cd = crmDeptDAO.findOne(crmDeptno);
		cd.setCrmDname(crmDname);
		crmDeptDAO.save(cd);

	}
}
